<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EmployeesController
 *
 * @author Admin
 */
class EmployeesController extends AppController{
    public function  table_employee() {   
        $this->set('title_for_layout', 'Table of Employee');
        $this->set('employees', $this->Employee->find('all'));        
    }

    public function add_employee() {
        $this->set('title_for_layout', 'Add Employee');
        if ($this->request->is('post')) {
            $this->Employee->create();
            if ($this->Employee->save($this->request->data)) {
                $this->Session->setFlash('A new employee has been saved.');
                $this->redirect(array('action' => 'table_employee'));
            } else {
                $this->Session->setFlash('Unable to add the employee.' .$this->request->data[0]);
            }
        }
    }

    public function edit_employee($id = null) {
        $this->set('title_for_layout', 'Edit Employee');
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $employee = $this->Employee->findById($id);
        if (!$employee) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->Employee->id = $id;
            if ($this->Employee->save($this->request->data)) {
                $this->Session->setFlash('The information of the employee has been updated.');
                $this->redirect(array('action' => 'table_employee'));
            } else {
                $this->Session->setFlash('Unable to update the information.');
            }
        }
        if (!$this->request->data) {
            $this->request->data = $employee;
        }
    }

    public function delete_employee($id) {
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Employee->delete($id)) {
            $this->Session->setFlash('The employee with id: ' . $id . ' has been deleted.');
            $this->redirect(array('action' => 'table_employee'));
        }
    }
}

?>
